package com.jhd.fuzhou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.EditTextView;
import com.jhd.fuzhou.view.daily.DailyStepTwoActivity;
import com.jhd.fuzhou.vo.Supervisor;

import java.util.List;

import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

public class SupervisorAdapter extends RecyclerView.Adapter<SupervisorAdapter.ViewHolder> {
    private List<Supervisor> supervisorList;
    private TextView textView;
    private TextView tv_Supervisor_1;
    private AlertDialog _alertDialog;
    private int checkPosition=-1;
    private Context context;
    public SupervisorAdapter(Context context, TextView textView,TextView tv_Supervisor_1, AlertDialog _alertDialog, List<Supervisor> supervisorList) {
        this.supervisorList = supervisorList;
        this._alertDialog = _alertDialog;
        this.textView = textView;
        this.context = context;
        this.tv_Supervisor_1 = tv_Supervisor_1;
    }
    @NonNull
    @Override
    public SupervisorAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_choose, parent, false);
        SupervisorAdapter.ViewHolder viewHolder = new SupervisorAdapter.ViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull SupervisorAdapter.ViewHolder holder, int position) {
        holder.tv_choose.setText(supervisorList.get(position).getName());
        if(supervisorList.get(position).getName().equals(textView.getText().toString())){
            holder.image.setVisibility(View.VISIBLE);
        }else{
            holder.image.setVisibility(View.GONE);
        }
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(supervisorList.get(position).getName());
                EditTextView.TextViewChange(textView,context);
                holder.image.setVisibility(View.VISIBLE);
                _alertDialog.dismiss();
                checkPosition=position;
                notifyDataSetChanged();
                if(textView==tv_Supervisor_1){
                    supervisionInspectionVo.setFirstSupervisorId(supervisorList.get(position).getId());
                    supervisionInspectionVo.setFirstSupervisorName(supervisorList.get(position).getName());
                    supervisionInspectionVo.setFirstSupervisorNo(supervisorList.get(position).getLawEnforceNo());
                }else{
                    supervisionInspectionVo.setSecondSupervisorId(supervisorList.get(position).getId());
                    supervisionInspectionVo.setSecondSupervisorName(supervisorList.get(position).getName());
                    supervisionInspectionVo.setSecondSupervisorNo(supervisorList.get(position).getLawEnforceNo());
                }
            }
        });
        if(position==checkPosition){
            holder.image.setVisibility(View.VISIBLE);
        }else{
            if(checkPosition!=-1){
                holder.image.setVisibility(View.GONE);
            }
        }
    }
    @Override
    public int getItemCount() {
        return supervisorList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_choose;
        ImageView image;
        RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            this.tv_choose = itemView.findViewById(R.id.tv_choose);
            this.image = itemView.findViewById(R.id.image);
            this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
        }
    }
}
